package org.speed.redislock;

import java.util.concurrent.TimeUnit;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.redisson.Redisson;
import org.redisson.api.RLock;

import com.google.gson.Gson;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
    	
    	/**
    	Redisson redisson = null;
    	
    	
    	String key = "sync_order_id_" + "110000111";
    	RLock lock = redisson.getLock(key);
    	try {
    		lock.tryLock(30, 30, TimeUnit.SECONDS);
    		
    		
    	} catch (Exception e) {
    		
    	}finally {
    		lock.unlock();
		}
    	**/
    	
    	GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
    	poolConfig.setMaxTotal(100);
    	poolConfig.setMaxIdle(200);
    	poolConfig.setMaxWaitMillis(2000);
    	poolConfig.setTestOnBorrow(true);
    	poolConfig.setTestWhileIdle(true);
    	
		JedisPool pool = new JedisPool(poolConfig , "127.0.0.1");
		Jedis jedis = pool.getResource();
		
		
		//String script = "local curVal = redis.call(\"get\", KEYS[1]); if curVal == false then curVal = 0 else curVal = tonumber(curVal) end; curVal = curVal * tonumber(ARGV[1]); redis.call(\"set\", KEYS[1], curVal); return curVal\n";
		//String script = "local curVal = redis.call(\"hget\",KEYS[1],KEYS[2]);if not curVal then curVal= {} else curVal['id']=1 end;return cjson.encode(curVal);";
		String script = "local curVal = redis.call(\"hget\",KEYS[1],KEYS[2]); local goodInfo = cjson.decode(curVal); return goodInfo['name']";

		
		Shop shop = new Shop();
		shop.setAge(1);
		shop.setId("1");
		shop.setName("yyb");
		
		
		Gson gson = new Gson();
		
		int keys = 2;
		String[] arguments = {"yyb","apple"};
		Object rs = jedis.eval(script, keys , arguments);
		System.out.println(rs);
		
		
		
		
        System.out.println( "Hello World!" );
    }
    
    static class Shop {
    	
    	private String id;
    	private int age;
    	private String name;
    	
		public String getId() {
			return id;
		}
		public void setId(String id) {
			this.id = id;
		}
		public int getAge() {
			return age;
		}
		public void setAge(int age) {
			this.age = age;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
    }
}
